package com.ml.mall.api.shop;

import com.ml.mall.bean.base.ApiRestResult;
import com.ml.mall.bean.req.product.*;
import com.ml.mall.bean.req.shop.ShopSupplierQueryDto;
import com.ml.mall.bean.req.variant.VariantRecommendQueryShopReq;
import com.ml.mall.consts.CommonConsts;
import com.ml.mall.consts.PermissionConst;
import com.ml.mall.entity.sys.SysUser;
import com.ml.mall.security.JwtUtil;
import com.ml.mall.service.shop.ShopRecommendService;
import com.ml.mall.service.shop.ShopService;
import com.ml.mall.service.variant.SysProductVariantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "【product】商店")
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    private ShopService shopService;

    @Autowired
    private SysProductVariantService sysProductVariantService;

    @Autowired
    private ShopRecommendService shopRecommendService; //推荐商品

    @ApiOperation(value = "商店分页查询卖家")
    @PostMapping("/query_supplier_page")
//    @RequiresPermissions(PermissionConst.SHOP_QUERY_SUPPLIER_PAGE)
    public ApiRestResult shopSupplierQueryPage(@RequestBody ShopSupplierQueryDto dto){
        SysUser sysUser = JwtUtil.getCurrentUser();
        if(sysUser.getRoleType().equals(CommonConsts.ROLE_TYPE_PURCHASE)){
            dto.setCompanyId(sysUser.getCompanyId());
        }
        return ApiRestResult.ok(shopService.queryPageShopSupplier(dto));
    }

    @ApiOperation(value = "商店查询推荐商品；")
    @PostMapping("/query_recommond")
//    @RequiresPermissions(PermissionConst.SHOP_QUERY_RECOMMOND)
    public ApiRestResult queryRecommend(@RequestBody VariantRecommendQueryShopReq dto){
        return ApiRestResult.ok(shopRecommendService.queryVariantRecommendShop(dto));
    }

    @RequestMapping(method = RequestMethod.POST, path = "/shop_query_page_by_keyword_states")
    @ApiOperation("通过关键词和州属id进行产品搜索")
//    @RequiresPermissions(PermissionConst.SHOP_SHOP_QUERY_PAGE_BY_KEYWORD_STATES)
    public ApiRestResult sysProductShopQueryPageByKeyWordAndStates(@RequestBody ShopQueryPageByKeyWord dto) {
        SysUser sysUser = JwtUtil.getCurrentUser();
        if(sysUser.getRoleType().equals(CommonConsts.ROLE_TYPE_PURCHASE)){
            dto.setCompanyId(sysUser.getCompanyId());
        }
        return ApiRestResult.ok(sysProductVariantService.sysProductQueryByKeywordAndStates(dto));
    }

    @RequestMapping(method = RequestMethod.POST, path = "/shop_query_page_by_category")
    @ApiOperation("通过目录id进行产品搜索")
//    @RequiresPermissions(PermissionConst.SHOP_SHOP_QUERY_PAGE_BY_CATEGORY)
    public ApiRestResult sysProductShopQueryPageByCategoryId(@RequestBody ShopQueryPageByCategoryIdDto dto) {
        return ApiRestResult.ok(sysProductVariantService.sysProductQueryPageByCategoryId(dto));
    }

    @RequestMapping(method = RequestMethod.POST, path = "/shop_query_sales")
    @ApiOperation("随机选取18条促销产品进行产品搜索")
//    @RequiresPermissions(PermissionConst.SHOP_SHOP_QUERY_SALES)
    public ApiRestResult sysProductShopQueryByRandomWith18(@RequestBody QueryRandom18Dto dto) {
        return ApiRestResult.ok(sysProductVariantService.sysProductQueryByRandomWith18(dto));
    }

    @RequestMapping(method = RequestMethod.POST, path = "/shop_product_query_one")
    @ApiOperation("根据variant_id查询单个产品的详细信息")
//    @RequiresPermissions(PermissionConst.SHOP_SHOP_PRODUCT_QUERY_ONE)
    public ApiRestResult sysProductShopQueryOneByVariantId(@RequestBody VariantIdDto dto) {
        return ApiRestResult.ok(sysProductVariantService.sysProductQueryOneByVariantId(dto));
    }

    @RequestMapping(method = RequestMethod.POST, path = "/shop_query_by_supplier")
    @ApiOperation("根据supplierId进行产品搜索")
//    @RequiresPermissions(PermissionConst.SHOP_SHOP_QUERY_BY_SUPPLIER)
    public ApiRestResult sysProductShopQueryPageBySupplierId(@RequestBody ShopQueryPageBySupplierIdDto dto) {
        SysUser sysUser = JwtUtil.getCurrentUser();
        if(sysUser.getRoleType().equals(CommonConsts.ROLE_TYPE_PURCHASE)){
            dto.setCompanyId(sysUser.getCompanyId());
        }
        return ApiRestResult.ok(sysProductVariantService.sysProductQueryPageBySupplierId(dto));
    }

    @RequestMapping(method = RequestMethod.POST, path = "/shop_query_relation")
    @ApiOperation("根据variantId查询同一子目录产品，并随机返回12个")
//    @RequiresPermissions(PermissionConst.SHOP_SHOP_QUERY_RELATION)
    public ApiRestResult sysProductShopQueryRandom12ByVariantId(@RequestBody SysVariantIdDto dto) {
        return ApiRestResult.ok(sysProductVariantService.sysProductQueryRandom12ByVariantId(dto));
    }
}
